package com.globallogic.socialnetwork.jsf.bean;

import java.util.ArrayList;
import java.util.List;


import javax.ejb.EJB;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;

import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.DaoException;
import com.globallogic.socialnetwork.util.WebApplication;


/**
 * JSF Bean to handle all functions associated with Friends
 */
@Named("friendBean")
@Scope("session")
public class FriendBean {

	@EJB(mappedName="java:module/UserService")
	private UserService userService;
	private String userLogin;
	private List<UserBean> friends;
	private List<UserBean> newFriends;
	private User selectedFriend;
	private final static Logger log = Logger.getLogger("SocialNetworkFileLogger");

	public FriendBean() {
		userLogin = WebApplication.getSessionAttribute("userlogin").toString();
	}

	/**
	 * Accepts request on adding to friends
	 */
	public String accept() {
		try {
			userService.acceptFriend(userLogin, selectedFriend);
			selectedFriend = null;
		} catch (DaoException e) {
			log.warn(e); 
		}
		return "home";
	}

	/**
	 * Removes selected friend from the list of friends
	 */
	public void removeFriend() {
		try {
			if (selectedFriend != null){
				User user=userService.getUser(userLogin);
				userService.removeFriend(selectedFriend,user);}
		} catch (DaoException e) {
			log.warn(e);
		}
	}

	/**
	 * Selects all friends of current User
	 * @return list of current User's friends
	 */
	public List<UserBean> getFriends() {
		try {
			List<User> allFriends = userService.getFriends(userLogin);
			if (allFriends != null) { 
				friends = new ArrayList<UserBean>();
				UserBean userBean=null;
				for (User friend : allFriends) {
					userBean = new UserBean(friend);
					friends.add(userBean);
				}
			} else { 
				friends = null;
			}
		} catch (DaoException e) {
			log.warn("");
		}
		return friends;
	}

	public void setFriends(List<UserBean> friends) {
		this.friends = friends;
	}

	/**
	 * Selects all people that have sent request to current user for adding to friends
	 * @return list of current followers that are not friends yet
	 */
	public List<UserBean> getNewFriends() {
		try {
			List<User> allNewFriends = userService.getNewFriends(userLogin);
			if (allNewFriends != null) {
				newFriends = new ArrayList<UserBean>();
				UserBean userBean;
				for (User friend : allNewFriends) {
					userBean = new UserBean(friend);
					newFriends.add(userBean);
				}
			} else {
				newFriends = null;
			}
		} catch (DaoException e) {
			log.warn("");
		}
		return newFriends;
	}

	public void setNewFriends(List<UserBean> newFriends) {
		this.newFriends = newFriends;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public User getSelectedFriend() {
		return selectedFriend;
	}
 
	public void setSelectedFriend(User selectedFriend) {
		this.selectedFriend = selectedFriend;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
